<?php
header("Content-type: text/html; charset=utf-8");

require_once('json.inc.php');
require('../dbconnect.inc.php');

$use_opt = false;

if($_GET['type'] == 'opt')
{
	$use_opt = true;
}

if(!empty($_GET['uid']))
{
	$rs = $dbh->query('SELECT gr . * FROM groups gr, users_groups ug WHERE ug.g_id = gr.g_id AND ug.u_id=' . intval($_GET['uid'])); 
}
else
{
	$rs = $dbh->query('select g_id, name from groups');
}

$alldb = $rs->fetchAll(PDO::FETCH_ASSOC);

foreach( $alldb as $key => $val)
{
	if ($use_opt)
	{
		echo "<option value='" . $val['g_id'] . "'>". $val['name'] ."</option>\n";
	}
	else
	{
		$response[$key]['id'] = $val['g_id'];
		$response[$key]['name'] = $val['name'];
	}
}

if (!$use_opt)
{
	echo json_encode($response);
}

?>
